package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.dao.bo.OrderDO;
import com.partners.dao.dto.DealOrderOutPutDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.OrderService;
import com.partners.utils.ApiResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**订单管理
 * @author liangyongle
 * @date 2018-12-21
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private OrderService orderService;


    /**
     * 获取订单列表
     * @param uid Integer
     * @return Object
     */
    @PostMapping("order_list")
    public ApiResult getOrderList(@BeanValid @RequestBody Paging paging,
                                  @SimpleParam("uid") Integer uid,
                                  @SimpleParam("returnType") Integer returnType){
        if(uid == null){
           throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        returnType = returnType!=null ? returnType : 0;
        Map<String,Object> list = orderService.getOrderList(paging,uid,returnType);
        return ApiResultUtil.ok(list);
    }

    /**
     * 新增订单
     * @param orderDO OrderDO
     *return Integer
    * */
    @PostMapping("addOrder")
    public ApiResult addOrder(@RequestBody OrderDO orderDO){

        if(orderDO.getCustomerId() == null){
            throw BusinessException.of(ErrorMessageEnum.CUSTOMERID_NOT_FOUND);
        }
        if(orderDO.getTotalMoney() == null){
            throw BusinessException.of(ErrorMessageEnum.TOTALMONEY_NOT_FOUND);
        }
        if(orderDO.getPic() == ""){
            throw BusinessException.of(ErrorMessageEnum.PIC_NOT_FOUND);
        }
        if (orderDO.getPoints() == null){
            throw BusinessException.of(ErrorMessageEnum.POINT_NOT_FOUND);
        }
        if(orderDO.getAddby() == null){
            throw BusinessException.of(ErrorMessageEnum.ADDUSER_NOT_FOUND);
        }
        Integer result =  orderService.createOrder(orderDO);
         return ApiResultUtil.ok(result);
    }

    /**
     * 获取下单记录列表
     * @param paging Paging
     *      uid Integer  //下单人id
     *      returnType  Integer  //是否按照月份输出
     *  return LIST
     * */
    @PostMapping("getDealOrder")
    public ApiResult getDealOrder(@BeanValid @RequestBody Paging paging,
                                  @SimpleParam("uid") Integer uid,
                                  @SimpleParam("returnType") Integer returnType){
        if(uid == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        returnType = returnType != null?returnType:0;
        Map<String,Object> list = orderService.getDealOrderList(paging,uid,returnType);
        return ApiResultUtil.ok(list);
    }


    /**
     * 获取下单记录详情
     * @param id Integer
     * return Object
     * */
    @PostMapping("dealOrderDetail")
    public ApiResult getDealOrderDetail(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        DealOrderOutPutDTO list = orderService.getDealOrderDetail(id);
        return ApiResultUtil.ok(list);
    }





}
